using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShadowPool : MonoBehaviour
{
    public static ShadowPool instance;  //创建单例
    
    public GameObject shadowObject;
    public int shadowCount;         //对象池中初始的object个数

    private Queue<GameObject> objectPool = new Queue<GameObject>();

    private void Awake()
    {
        instance = this;
        
        //初始化对象池
        FillPool();
    }

    void FillPool()
    {
        for (int i = 0; i < shadowCount; i++)
        {
            var shadow = Instantiate(shadowObject);
            shadow.transform.SetParent(transform);//将对象池中的物体设置为对象池的子物体
            
            //加入object到对象池
            EnPool(shadow);
        }
    }

    //EnPool要单独一个函数并写成Public，方便其他脚本调用
    //因为其他脚本从对象池中拿出对象后，要把物体放回到对象池
    public void EnPool(GameObject gameObject)
    {
        gameObject.SetActive(false);
        objectPool.Enqueue(gameObject);
    }

    public GameObject DePool()
    {
        if (objectPool.Count == 0)
        {
            FillPool();
        }
        var gameObject = objectPool.Dequeue();
        gameObject.SetActive(true);
        return gameObject;
    }
}
